<template>
  <div>
    <!-- 顶部栏 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.go(-1)" />
    <!-- 头像盒子 -->
    <div class="avatar">
      <van-image
        round
        fit="cover"
        width="120"
        height="120"
        :src="userInfo.photo"
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 编辑cell -->
    <van-cell-group>
      <van-cell
        title="名称"
        :value="userInfo.name"
        is-link
        @click="showDialogClick"
      />
      <van-cell
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        is-link
        @click="showPopup = true"
      />
      <van-cell
        title="生日"
        :value="userInfo.birthday"
        is-link
        @click="datePickerClick"
      />
    </van-cell-group>
    <!-- 修改名称弹窗 -->
    <!-- 添加showDidlog控制弹窗显示与隐藏 -->
    <van-dialog
      v-model="showDialog"
      title="编辑姓名"
      show-cancel-button
      @confirm="editName"
    >
      <!-- 添加newName:输入框文本双向绑定 -->
      <van-field ref="nameFiled" v-model="newName" placeholder="请输入名字" />
    </van-dialog>

    <!-- 编辑性别弹窗 -->
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar
        title="编辑性别"
        left-arrow
        @click-left="showPopup = false"
      />
      <van-cell title="男" is-link @click="editGender(0)" />
      <van-cell title="女" is-link @click="editGender(1)" />
    </van-popup>

    <!-- 编辑生日 -->
    <van-popup v-model="showDatePicker" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showDatePicker = false"
        @confirm="editBirthday"
      />
    </van-popup>

    <!-- 裁切头像图片 -->
    <div class="cropper-box" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCropWidth="120"
        autoCropHeigh="120"
        autoCrop
      >
      </vueCropper>
      <van-button type="primary" class="cancel" @click="showCropper = false"
        >取消</van-button
      >
      <van-button type="info" class="confirm" @click="editPhotoClick"
        >确定</van-button
      >
    </div>
  </div>
</template>

<script>
// 导入mapState辅助函数
import { mapState } from "vuex";
// 导入接口
import { editUserInfo } from "./user.js";
// 导入moment
import * as moment from "moment";
// 导入头像接口
import { editPhoto } from "./user.js";
// 导入vue-cropper
import { VueCropper } from "vue-cropper";
export default {
  name: "edit",
  data() {
    return {
      showDialog: false,
      newName: "",
      showPopup: false,
      // 编辑生日
      currentDate: new Date(),
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      showDatePicker: false,
      img: "",
      showCropper: false,
    };
  },

  components: {
    VueCropper,
  },

  methods: {
    // 1.点击编辑姓名按钮,弹出编辑姓名窗口
    showDialogClick() {
      // 设置输入框文本
      this.newName = this.userInfo.name;
      // 弹出dialog
      this.showDialog = true;
      // 自动成为焦点
      // this.$nextTick类似于高级定时器
      this.$nextTick(() => {
        this.$refs.nameFiled.focus();
      });
    },
    // 2.编辑姓名确认按钮,修改姓名
    editName() {
      console.log(this.newName);
      editUserInfo({ name: this.newName }).then((res) => {
        console.log(res);
        // (1)弹窗提示
        // toast为轻弹窗,没有交互效果
        this.$toast.success("修改姓名成功");
        // (2)更新vuex数据
        this.$store.commit("updateUserInfo", {
          // 先取出原先的属性
          ...this.userInfo,
          // 再单独覆盖name属性
          name: this.newName,
        });
      });
    },
    // 3.编辑性别
    editGender(gender) {
      // (1)发送请求
      editUserInfo({ gender }).then((res) => {
        console.log(res);
        // (2)隐藏popup弹窗
        this.showPopup = false;
        // (3)弹出提示框
        this.$toast.success("修改性别成功");
        // (4)更新vuex数据
        this.$store.commit("updateUserInfo", {
          ...this.userInfo,
          // gender:gebder解构
          gender,
        });
      });
    },
    // 4.点击生日cell
    datePickerClick() {
      this.showDatePicker = true;
      //  把vuex中的字符串生成date对象
      let date = new Date(this.userInfo.birthday);
      //  把日期对象给currentDate(这里不用转格式,因为vuex的格式是正确的)
      this.currentDate = date;
    },
    // 5.编辑生日
    editBirthday() {
      //  (1)转换日期格式
      let birthday = moment(this.currentDate).format("YYYY-MM-DD");
      // (2)发送请求
      editUserInfo({ birthday }).then((res) => {
        console.log(res);
        // (3)弹窗提示
        this.$toast.success("修改生日成功");
        // (4)更新vuex数据
        this.$store.commit("updateUserInfo", {
          ...this.userInfo,
          birthday,
        });
        // (5)隐藏popup弹窗
        this.showDatePicker = false;
      });
    },
    // 6.编辑头像
    afterRead(file) {
      // // (1)创建formdata对象
      // let fd = new FormData();
      // fd.append("photo", file.file);
      // // (2)发送请求
      // editPhoto(fd).then((res) => {
      //   console.log(res);
      //   // (3)弹窗提示
      //   this.$toast.success("更新头像成功");
      //   // (4)更新vuex数据
      //   this.$store.commit("updateUserInfo", {
      //     ...this.userInfo,
      //     photo: res.data.data.photo,
      //   });
      // });

      // (1)设置要裁切的图片
      this.img = file.content;
      // (2)显示裁切盒子
      this.showCropper = true;
    },
    // 7.裁切头像
    editPhotoClick() {
      // (1)获取裁切后的图片
      this.$refs.cropper.getCropBlob((data) => {
        console.log(data);
        // (2)创建formdata对象
        let fd = new FormData();
        fd.append("photo", data);
        // (3)发送请求
        editPhoto(fd).then((res) => {
          console.log(res);
          // (4)弹窗提示
          this.$toast.success("更新头像成功");
          // (5)隐藏cropper弹窗
          this.showCropper = false;
          // (6)更新vuex数据
          this.$store.commit("updateUserInfo", {
            ...this.userInfo,
            photo: res.data.data.photo,
          });
        });
      });
    },
  },
  computed: {
    // 将vuex数据隐射成计算属性
    ...mapState(["userInfo"]),
  },
};
</script>

<style lang="less">
.avatar {
  height: 160px;
  display: flex;
  justify-content: center;
  align-items: center;

  .van-uploader {
    opacity: 0;
    position: absolute;
    width: 125px;
    height: 125px;
    .van-uploader__upload {
      width: 120px;
      height: 120px;
    }
  }
}

.cropper-box {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  .cancel {
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .confirm {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>